#include <QColor>
#include "usermessage.h"

UserMessage::UserMessage(User newUsr)
{
	operation = USER_INFO;
	usr = newUsr;
}

/**
  * Populate the user object with that data from the QByteArray
  */
UserMessage::UserMessage(QByteArray bytes)
{
	operation = USER_INFO;

	int tmpInt;
	char* tmpChar = NULL;
	bool tmpBool;

	QDataStream sin(bytes);

	//get the operation
	quint8 op;
	sin >> op;
	operation = op;

	sin >> tmpInt;
	usr.setId(tmpInt);

	sin >> tmpChar;
	usr.setName(QString(tmpChar));
	delete [] tmpChar;
	tmpChar = NULL;

	sin >> tmpInt;
	usr.setColour(QColor((QRgb)tmpInt));

	sin >> tmpBool;
	usr.setOnlineStatus(tmpBool);

	sin >> tmpChar;
	usr.setIp(QString(tmpChar));
	delete [] tmpChar;
	tmpChar = NULL;

	sin >> tmpInt;
	usr.setPort(tmpInt);
}

QByteArray UserMessage::toBytes()
{
	QByteArray bytes;
	QDataStream sout(&bytes, QIODevice::WriteOnly);

	sout << operation << usr.getId() << usr.getName().toAscii();
	sout << usr.getColour().toRgb().rgb() << usr.getOnlineStatus();
	sout << usr.getIp().toAscii() << usr.getPort();

	return bytes;
}
